package me.youline.dataServer.service.pubMsg;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import me.youline.dataServer.dao.PubMsgDAO;
import me.youline.dataServer.dao.PubMsgIndexDAO;
import me.youline.dataServer.dao.PubMsgStatisticRecorderDAO;
import me.youline.dataServer.dao.UserSocialRelationDAO;
import me.youline.dataServer.entity.PubMsgStatisticRecorder;
import me.youline.dataServer.entity.PublicMessage;
import me.youline.dataServer.entity.UserSocialRelation;
import me.youline.dataServer.entity.PublicMessage.MsgStatus;
import me.youline.dataServer.entity.PublicMessageIndex;
import me.youline.dataServer.utils.IdPagination;

import org.perf4j.aop.Profiled;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.base.Function;
import com.google.common.collect.Lists;

@Component
public class PubMsgQueryService {
	
	@Autowired
	private PubMsgDAO publicMessageDAO;
	
	@Autowired
	private PubMsgIndexDAO publicMessageIndexDAO;
	
	@Autowired
	private PubMsgStatisticRecorderDAO msgRecorderDAO; 
	
	@Autowired
	private UserSocialRelationDAO socialRelationDAO;
	
	/**
	 * 获取公告详细信息（包括点赞数等）
	 * @param messageId
	 * @return
	 */
	@Transactional
	@Profiled(tag = "service-根据消息ID获取消息对象")
	public PublicMessage getPubMsgDetailInfo(Long messageId) {
		PublicMessage m = publicMessageDAO.findOne(messageId);
		if(m == null){
			return null;
		}
		PubMsgStatisticRecorder r = msgRecorderDAO.findOne(messageId);
		m.setMessageRecorder(r);
		return m;
	}

	/**
	 * 获取公告详细信息列表
	 * @param messageIds
	 * @return
	 */
	@Transactional
	public List<PublicMessage> getPubMsgDetailInfos(List<Long> messageIds) {
		List<PublicMessage> list = new ArrayList<PublicMessage>(
				messageIds.size());
		for (Long id : messageIds) {
			PublicMessage g = this.getPubMsgDetailInfo(id);
			if (g != null) {
				list.add(g);
			}
		}
		return list;
	}
	
	
	
	/**
	 * 获取个人主页索引
	 * @param viewUserId
	 * @param userId
	 * @param page
	 * @return
	 */
	public List<PublicMessageIndex> getPersonalMsgIndex(Long viewUserId,
			Long userId, IdPagination page) {
		List<PublicMessageIndex> personalMsgIndex = null;
		personalMsgIndex = publicMessageIndexDAO.getMsgIdsByUserId(userId,
					MsgStatus.publicMsg, page);// 获取个人发送的消息包括尽自己可见以及公开
		return personalMsgIndex;
	}
	
	/**
	 * 分页查询所有公告索引，且按插入时间降序排序（最新的公告排于前面）
	 * @param idPagination
	 * @return
	 */
	public List<PublicMessageIndex> getAllMsgIndex(IdPagination idPagination){
		return publicMessageIndexDAO.getMsgIndexs(idPagination);
	}
	
	/**
	 * 获取指定用户关注人所发的公告
	 * @param userId
	 * @param page
	 * @return
	 */
	public List<PublicMessageIndex> getFollowingMsgList(Long userId,IdPagination page){
		List<UserSocialRelation> rels = socialRelationDAO.getUserFollowingRels(
				userId, null);// 获取关注关系列表
		List<Long> followings = Lists.transform(rels,
				new Function<UserSocialRelation, Long>() {// 获取关注人id列表
					@Override
					public Long apply(UserSocialRelation input) {
						return input.getToUserId();
					}
				});
		List<PublicMessageIndex> followingMsgIndexs = this.getMsgIdsByUserIds(followings, page);// 获取关注人所发消息
		Collections.sort(followingMsgIndexs, Collections.reverseOrder());// 降序排序id，由于最新的消息id较大，按此排序获取最新的消息。
		return followingMsgIndexs;
	}
	
	/**
	 * 分页获取指定用户所发的消息
	 * 
	 * @param userIds
	 * @param page
	 * @return
	 */
	protected List<PublicMessageIndex> getMsgIdsByUserIds(List<Long> userIds,
			IdPagination page) {
		if (userIds == null || userIds.isEmpty()) {
			return Collections.emptyList();
		} else {
			return publicMessageIndexDAO.getPublicMsgIdsByUserIds(
					new ArrayList<Long>(userIds), page);
		}
	}
	
}
